---
title: "Example"
author: "Yuan Zhou"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output: html_document
---
  
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE, collapse = FALSE)
```

```{r}
library(quanteda)
library(newsmap)

# train the model
dict <- dictionary(file = "topic_low-entropy.yml")
toks <- readRDS("data/data_tokens_sent.RDS") %>% 
  tokens_subset(year >= 1991)
toks <- tokens_compound(toks, dict, join = FALSE, concatenator = " ")

mt_key <- dfm(tokens_lookup(toks, dict, levels = 1))
mt <- dfm(toks, remove = c("", stopwords("en"))) %>% 
  dfm_trim(min_termfreq = 10)
map <- textmodel_newsmap(mt, mt_key)

# predict
p5 <- c("USA", "CHN", "RUS", "GBR", "FRA")
mt_p5 <- dfm_subset(mt, country %in% p5)
mt_p5 <- mt_p5[rowSums(mt_p5) > 0, ]
dat_p5 <- docvars(mt_p5)
rownames(dat_p5) <- docnames(mt_p5)
dat_p5$country <- factor(dat_p5$country,
                        levels = c("USA", "CHN", "RUS", "GBR", "FRA"))
dat_p5$topic <- factor(predict(map, newdata = mt_p5),
                       levels = c("greeting", "un", "human", "democracy", "development", "security"))
dat_p5$smoothed <- NA
for (d in levels(droplevels(dat_p5$docname))) {
  #cat(d, "\n")
  mt_temp <- dfm_subset(mt_p5, docname == d)
  mt_pred <- as.matrix(predict(map, newdata = mt_temp, type = "all"))
  mt_pred[is.na(mt_pred)] <- 0
  mt_smooth <- kernapply(mt_pred, kernel("daniell", c(3, 3)))
  dat_p5[rownames(mt_smooth),]$smoothed <- colnames(mt_smooth)[max.col(mt_smooth)]
}
dat_p5$smoothed <- factor(dat_p5$smoothed,
                          levels = c("greeting", "un", "human", "democracy", "development", "security"))
```


```{r fig.height=4, fig.width=8}
tb_pre <- xtabs(~ country + smoothed, dat_p5, year < 2001)
tb_post <- xtabs(~ country + smoothed, dat_p5, year >= 2001)

par(mfrow = c(1, 2), mar = c(1, 1, 3, 1), cex.main = 1)
mosaicplot(tb_pre, main = "1991–2000", color = gray.colors(5), 
           las = 1, xlab = "", ylab = "")
mosaicplot(tb_post, main = "2001–2017", color = gray.colors(5), 
           las = 1, xlab = "", ylab = "")
```
